/**
 * 
 */
package com.xb.concurrent.thread.stop;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author kexue
 *
 */
public class TestInterrupt {

	private static Logger logger = LoggerFactory.getLogger(TestInterrupt.class);
	private static int startNo=0;
	private final static ConcurrentHashMap<File, Long> fileSizeCount = new ConcurrentHashMap<File, Long>();

	public static void main(String[] args) throws InterruptedException { 
		File baseDir = new File("D:\\迅雷下载\\test");
		ExecutorService executorService = Executors.newFixedThreadPool(2);
		sumitFile (baseDir, executorService);
		logger.info("submit completed");

		executorService.shutdownNow();
		logger.info("executorService.shutdown();");
		
		
		boolean isTerminated = executorService.isTerminated();
		logger.info(isTerminated + "");
		
		//executorService.awaitTermination(1, TimeUnit.MINUTES); 
		logger.info(fileSizeCount.toString());
	}

	/**
	 * @param
	 * @param executorService
	 */
	private static void sumitFileSleep(File file, ExecutorService executorService) {

		if(!file.exists()) return;
		if(file.isDirectory()){
			for(File ele:file.listFiles()){
				sumitFileSleep(ele, executorService);
			}
		}else{
			TaskSleep task = new TaskSleep(file, startNo++);
			executorService.submit(task);
		}
		
	}
	/**
	 * @param
	 * @param executorService
	 */
	private static void sumitFile(File file, ExecutorService executorService) {

		if(!file.exists()) return;
		if(file.isDirectory()){
			for(File ele:file.listFiles()){
				sumitFile(ele, executorService);
			}
		}else{
			Task task = new Task(file, startNo++);
			executorService.submit(task);
		}
		
	}

	static class Task implements Runnable {

		private Integer taskNo;// 任务编号
		private File file;

		/**
		 * @param taskNo
		 */
		public Task(File file, Integer taskNo) {
			super();
			this.taskNo = taskNo;
			this.file = file;
		}

		public void run() {
			logger.info("task-no :" + taskNo + " started [" + file.getAbsolutePath() + "]");
			// TODO Auto-generated method stub
			Long count = readFileCount(file);
			fileSizeCount.put(file, count);
			logger.info("task-no :" + taskNo + "completed");
		}

		/**
		 * @param file2
		 * @return
		 */
		private Long readFileCount(File file2) {
			if (file2 != null && file2.exists()) {
				BufferedInputStream bufferedInputStream = null;
				try {
					bufferedInputStream = new BufferedInputStream(new FileInputStream(file2));
					byte[] b = new byte[1024*4];//1mb
					int len = 0;
					long sum = 0;
					while ((len = bufferedInputStream.read(b)) > 0) {
						sum = sum + len;
						logger.info("task-no :" + taskNo + " read " +len);
					}
					return sum;
				} catch (IOException e) {
					throw new RuntimeException(e);
				} finally {
					try {
						bufferedInputStream.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			return null;
		}
	}

	
	
	static class TaskSleep implements Runnable {

		private Integer taskNo;// 任务编号
		private File file;

		/**
		 * @param taskNo
		 */
		public TaskSleep(File file, Integer taskNo) {
			super();
			this.taskNo = taskNo;
			this.file = file;
		}

		public void run() {
			logger.info("task-no :" + taskNo + " started [" + file.getAbsolutePath() + "]");
			try {
				Thread.currentThread().sleep(1000 * 120);
			} catch (InterruptedException e) {
				Thread.currentThread().interrupt();
			}
			logger.info("task-no :" + taskNo + "completed");
		}
		  
	}
}
